package com.iot.inclinometer.common;

import android.util.Log;

import org.xutils.DbManager;
import org.xutils.db.table.TableEntity;

import java.io.File;

import static com.iot.inclinometer.util.BaseValue.DATABASE_FILE_PATH;
import static com.iot.inclinometer.util.BaseValue.DATABASE_NAME;
import static com.iot.inclinometer.util.BaseValue.DATABASE_VERSION;

/**
 * @Author: Song Yu
 * @Date: 2021/4/12 0012 18:56
 */
public class DbConfig {
    private static DbManager.DaoConfig daoConfig;

    public static DbManager.DaoConfig getDaoConfig() {
        if (daoConfig == null) {
            DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                    //设置数据库名，默认xutils.db
                    .setDbName(DATABASE_NAME)
                    //设置数据库路径，默认存储在app的私有目录
                    .setDbDir(new File(DATABASE_FILE_PATH))
                    //设置数据库的版本号
                    .setDbVersion(DATABASE_VERSION)
                    //设置数据库打开的监听
                    .setDbOpenListener(new DbManager.DbOpenListener() {
                        @Override
                        public void onDbOpened(DbManager db) {
                            //开启数据库支持多线程操作，提升性能，对写入加速提升巨大
                            db.getDatabase().enableWriteAheadLogging();
                        }
                    })
                    //设置数据库更新的监听
                    .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                        @Override
                        public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        }
                    })
                    //设置表创建的监听
                    .setTableCreateListener(new DbManager.TableCreateListener() {
                        @Override
                        public void onTableCreated(DbManager db, TableEntity<?> table) {
                            Log.i("JAVA", "onTableCreated：" + table.getName());
                        }
                    });
        }
        return daoConfig;
    }
}
